﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.user.Edit_Entities
{
    public partial class EditCustomer: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ErrorLabel.Visible = false;
        }

        protected void CustomerDetailsView_DataBound(object sender, EventArgs e)
        {
            //George , in the item template , i changed the label names
            //so it would be easier to find them
            Label type = (Label)CustomerDetailsView.FindControl("TypeLabel");
            if (type != null)
            {
                CustomerTypeBLL custometTypeBLL = new CustomerTypeBLL(); ;
                try
                {
                    type.Text = custometTypeBLL.CustomerTypeGetByNum(new Guid(type.Text))[0].customer_type;
                }
                finally
                {
                    //TODO
                    //we should add dispose here
                    // we need to add dispose function foreach bll
                    // or maybe the detailsview disposes alone ,
                    // need to check, because there exists disposing event
                }
            }
		}

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            CustomerBLL BLL = new CustomerBLL();
            String Previous = BLL.CustomerGetByNum((Guid)CustomerDetailsView.SelectedValue)[0].customer_id;


            if (Previous != args.Value)
            {
                try
                {
                    if (BLL.CustomerGetById(args.Value).Rows.Count > 0)
                        args.IsValid = false;
                    else
                        args.IsValid = true;
                }
                finally
                {
                    //TODO: Dispose
                }

            }
        }

        protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
        {
            CustomerBLL BLL = new CustomerBLL();
            String Previous = BLL.CustomerGetByNum((Guid)CustomerDetailsView.SelectedValue)[0].customer_name;


            if (Previous != args.Value)
            {
                try
                {
                    if (BLL.CustomerGetByName(args.Value).Rows.Count > 0)
                        args.IsValid = false;
                    else
                        args.IsValid = true;
                }
                finally
                {
                    //TODO: Dispose
                }
            }

        }

        protected void CustomerDetailsView_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            try
            {
                Label Label = (Label)CustomerDetailsView.FindControl("CustomerLabel");
                CustomerBLL BLL = new CustomerBLL();
                Guid guid = BLL.CustomerGetById(Label.Text)[0].customer_num;

                LendBLL CBLL = new LendBLL();
                if (CBLL.LendGetByCustomer(guid).Rows.Count > 0)
                {
                    e.Cancel = true;
                    ErrorLabel.Visible = true;
                }

                DeliveredBLL DBLL = new DeliveredBLL();
                if (DBLL.DeliveredGetByCustomer(guid).Rows.Count > 0)
                {
                    e.Cancel = true;
                    ErrorLabel.Visible = true;
                }


            }
            finally
            {
                //TODO: Dispose
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

 
    }
}
